<template>
  <div>
    <!-- 索赔审核原因维护--车厂 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
        <multiuse-button type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
        <multiuse-button size="small" type="primary" :click="downloadTemplate.bind()">下载模板</multiuse-button>
        <multiuse-button size="small" icon="el-icon-upload" @click="showImportFun()">导入</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleAdd" title="审核原因新增" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </BaseDialog>
    <BaseDialog :visible.sync="emitVisible" title="审核原因编辑" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <editForm @close="emitCloseHandle" @addChange="emitChange" :rowData="rowData" />
    </BaseDialog>
    <BaseDialog :visible.sync="showImport" :closable="false" title="导入" icon="el-icon-upload" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">
      <importExcel @close="showImport = false" @importclose="editsave1()" url="/api/cms_claimfac/rPCheckReason/importExcel"/>
    </BaseDialog>
  </div>
</template>
<script>
import { getListByPage, newSave, piliangqiyong, piliangtingyong, excelExport, editSave ,downloadModel,getListForExport,exporturl} from '@/api/claim/factory/ClaimCheckReasonMaintain';
import AddForm from './AddForm.vue';
import { getPartType } from '@/api/dictionary/index.js';
// import importSuoPeiShenHeYuanYinWeiHu from './importSuoPeiShenHeYuanYinWeiHu.vue';
import importExcel from '@/pages/claimDemage/tools/importExcel.vue';
import editForm from './editForm.vue';
import { exportExcelFile } from '@/utils/index';
export default {
  components: {
    AddForm,
    editForm,
    importExcel
  },
  data() {
    return {
      visible: false,
      showImport: false,
      collapse: false,
      emitVisible: false,
      rowData: {},
      fetchapi: getListByPage,
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中的行
      visibleAdd: false
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    async chuqi() {
      const res = await getPartType({ type: 5582 });
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'reasonType').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'reasonType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
      const res2 = await getPartType({ type: 5583 });
      if (res2.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'reasonCol').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'reasonCol').itemList = res2.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    //下载模板
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        downloadModel().then(res => {
          console.log(res);
          exportExcelFile(res.data, '索赔审核原因维护导入模板');
        });
      });
    },
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000103' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    add() {
      this.visibleAdd = true;
    },
    emitFun(val) {
      this.rowData = val;
      this.emitVisible = true;
    },
    emitCloseHandle() {
      this.emitVisible = false;
    },
    emitChange() {
      this.emitVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    editsave1() {
      this.showImport = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
      console.log(val);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'SELECT', label: '单据类型', fieldName: 'reasonType', placeholder: '请选择单据类型...', initialValue: '', filterable: true, itemList: []},
        { type: 'SELECT', label: '索赔字段', fieldName: 'reasonCol', placeholder: '请选择索赔字段...', initialValue: '', filterable: true, itemList: []},
        { type: 'INPUT', label: '审核原因', fieldName: 'reasonRemarks', placeholder: '请输入审核原因...', initialValue: '' }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.emitFun(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '状态', dataIndex: 'statusStr', sorter: true, width: 80},
        { title: '单据类型', dataIndex: 'reasonTypeStr', sorter: true },
        { title: '索赔字段', dataIndex: 'reasonColStr', sorter: true },
        { title: '审核原因', dataIndex: 'reasonRemarks', sorter: true },
        { title: '创建人', dataIndex: 'createdBy', sorter: true },
        { title: '创建时间', dataIndex: 'createdAt', sorter: true },
        { title: '修改人', dataIndex: 'updatedBy', sorter: true },
        { title: '修改时间', dataIndex: 'updatedAt', sorter: true }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
